package com.tools.web.work.captcha;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 验证码实体构建对象
 * */
@Note("验证码实体构建对象")
public class CaptchaBuilder {

    @Note("图片验证码的宽度。默认 130px")
    private int width = 130;

    @Note("图片验证码的高度。默认 48px")
    private int height = 48;

    @Note("验证码的位数。默认 4 位。最低也要 4 位")
    private int length = 4;

    @Note("允许错误的次数。大于 0 才生效")
    private int allowErrorCount;


    public CaptchaBuilder() {}

    public CaptchaBuilder(int length) {
        this.setLength(length);
    }

    public CaptchaBuilder(int length, int allowErrorCount) {
        this.setLength(length);
        this.setAllowErrorCount(allowErrorCount);
    }

    public CaptchaBuilder(int width, int height, int length) {
        this.setWidth(width);
        this.setHeight(height);
        this.setLength(length);
    }

    public CaptchaBuilder(int width, int height, int length, int allowErrorCount) {
        this.setWidth(width);
        this.setHeight(height);
        this.setLength(length);
        this.setAllowErrorCount(allowErrorCount);
    }

    public static CaptchaBuilder of() {
        return new CaptchaBuilder();
    }

    public static CaptchaBuilder of(int length) {
        return new CaptchaBuilder(length);
    }

    public static CaptchaBuilder of(int length, int allowErrorCount) {
        return new CaptchaBuilder(length, allowErrorCount);
    }

    public static CaptchaBuilder of(int width, int height, int length) {
        return new CaptchaBuilder(width, height, length);
    }

    public static CaptchaBuilder of(int width, int height, int length, int allowErrorCount) {
        return new CaptchaBuilder(width, height, length, allowErrorCount);
    }


    public void setWidth(int width) {
        if(width < 1) return;
        this.width = width;
    }

    public int getWidth() {
        return width;
    }

    public void setHeight(int height) {
        if(height < 1) return;
        this.height = height;
    }

    public int getHeight() {
        return height;
    }

    public void setLength(int length) {
        if(length <= this.length) return;
        this.length = length;
    }

    public int getLength() {
        return length;
    }

    public int getAllowErrorCount() {
        return allowErrorCount;
    }

    public void setAllowErrorCount(int allowErrorCount) {
        this.allowErrorCount = allowErrorCount;
    }

    @Override
    public String toString() {
        return "CaptchaBuilder{" +
                "width=" + width +
                ", height=" + height +
                ", length=" + length +
                ", allowErrorCount=" + allowErrorCount +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CaptchaBuilder that = (CaptchaBuilder) o;
        return width == that.width && height == that.height && length == that.length && allowErrorCount == that.allowErrorCount;
    }

    @Override
    public int hashCode() {
        return Objects.hash(width, height, length, allowErrorCount);
    }

    /* ********************************************************************************************
     *
     *          链式方法
     *
     * *********************************************************************************************
     * */

    public CaptchaBuilder width(int width) {
        this.setWidth(width);
        return this;
    }

    public CaptchaBuilder height(int height) {
        this.setHeight(height);
        return this;
    }

    public CaptchaBuilder length(int length) {
        this.setLength(length);
        return this;
    }

    public CaptchaBuilder allowErrorCount(int allowErrorCount) {
        this.setAllowErrorCount(allowErrorCount);
        return this;
    }
}
